<template>
	<div class="content">
		<h2><i class="iconfont icon-jingli"></i>区域经理审核管理</h2>
		<div class="content-wrap">
			<!--搜索分类-->
			<div class="search">
				<div class="search__list">
					<div class="search__list--title search__list--title-tip">审核状态</div>
					<el-select v-model="params.check_state" @change="selctCheckState" size="mini">
						<el-option label="全部" :value="''"></el-option>
						<el-option label="未审核" :value="0"></el-option>
						<el-option label="已审核" :value="1"></el-option>
					</el-select>
				</div>
				<div class="search__list">
					<div class="search__list--title">审核时间</div>
					<el-date-picker
							@change="handleSelectTimes"
						    v-model="times"
						    type="daterange"
						    range-separator="至"
						    start-placeholder="开始日期"
						    end-placeholder="结束日期"
						    size="mini">
				  	</el-date-picker>
				</div>
			</div>
			<!--商品展示表格-->
			<div v-loading="loading" >
				<el-table :data="data" strip size="mini">
					<el-table-column label="用户名称" min-width="120">
						<template slot-scope="scope">
							<div class="img-desc">
								<img :src="scope.row.member.head_img" class="img-desc__img img-desc__img-round" style="width: 40px; height: 40px;"/>
								<span>{{scope.row.member.real_name}}</span>	
							</div>
						</template>
					</el-table-column>
					<el-table-column label="电话" prop="member.mobile"></el-table-column>
					<el-table-column label="申请日期" prop="ask_time">
						<template slot-scope="scope">
							<span>{{ scope.row.ask_time | formatTime }}</span>
						</template>
					</el-table-column>
					<el-table-column label="审核日期" prop="check_time" >
						<template slot-scope="scope">
							<span>{{ scope.row.ask_time | formatTime }}</span>
						</template>
					</el-table-column>
					<el-table-column label="申请类型" >
						<template slot-scope="scope">
							<el-tag v-if="scope.row.ask_type === 1" size="small">推广大使</el-tag >
							<el-tag v-else-if="scope.row.ask_type === 2" size="small">区域经理</el-tag >
						</template>
					</el-table-column>
					<el-table-column label="状态" align="center">
						<template slot-scope="scope">
							<el-tag type="warning" v-if="!scope.row.check_time" size="small">等待审核</el-tag>
							<el-tag 
								v-else-if="scope.row.is_agree === 0"
								type="danger"
								:title="scope.row.reject_reason ? scope.row.reject_reason : ''"
								 size="small">审核驳回</el-tag>
							<el-tag type="success" v-else size="small">审核通过</el-tag>
						</template>
					</el-table-column>
					
					<el-table-column label="操作" align="center" width="250">
						<template slot-scope="scope" size="mini">
							<el-button 
								size="mini"
								@click="agreeManagerNoArea(scope.row)"
								 type="success" 
								 v-if="!scope.row.check_time">
								 同意
							</el-button>
							<el-button 
								size="mini"
								@click="rejectManager(scope.row)"
								 type="danger" 
								 v-if="!scope.row.check_time">
								 拒绝
							</el-button>
							<el-button size="mini" @click="showDetail(scope.row)">详情</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<el-pagination 
				@current-change="handleCurrentChange"
				background 
				layout="total, prev, pager, next, jumper"
				:total="totalNum" 
				class="fr"
				size="mini"
				>
			</el-pagination>
			<s-model v-if="showModel" :id="saleman_id"></s-model>
			<select-area v-if="showAgreeModel" :id="be_saleman_id"></select-area>
		</div>
	</div>
</template>

<script>
	import model from "./manager_detail.vue"
	import select_area from "./select_area.vue"
	export default {
		created() {
			this.fetchIsManager();
		},
		components: {
			"s-model": model,
			"select-area": select_area
		},
		data() {
			return {
				params: {
					page: 1,
					rows: 10,
					start_time: "",
					end_time: "",
					check_state: '',			//审核状态
				},
				reject_reason: null,		//拒绝理由
				showModel: false,			//显示申请详情
				showAgreeModel: false,
				data: null,					//区域经理数据			
				times: "",					//申请日期
				loading: true,				//加载小圈圈
				saleman_id: null,			//申请区域经理用户的id
				be_saleman_id: null,
				totalNum: 0					//返回数据总数，用于分页那里显示
			}
		},
		methods: {
			//翻页
			handleCurrentChange(pages) {
				this.params.page = pages;
				this.fetchIsManager();
			},
			//选择时间时候触发
			handleSelectTimes(val) {
				if (this.times) {
					this.params.start_time = this.times[0].getTime();
					this.params.end_time = this.times[1].getTime();
				}else {
					this.params.start_time = "";
					this.params.end_time = "";
				}
				this.fetchIsManager();

			},
			//选择审核状态0代表未审核，1代表已审核
			selctCheckState(val) {
				this.params.check_state = val;
				this.fetchIsManager();
			},
			//获取区域经理或者推广大使
			fetchIsManager() {
				this.loading = true;
				var url = `${this.url}/spread/check_ask_manager_list.action`;
				var params = this.params;
				this.$http.get(url,{ params }).then((res)=> {
					this.totalNum = res.data.result.total;
					this.loading = false;
					this.data = res.data.result.rows;
				});
			},
			//显示申请详情
			showDetail(row) {
				if (row.ask_type === 1) {
					
				}else if (row.ask_type === 2){
					this.saleman_id = row.be_saleman_id;
					this.showModel = true;
				}
				
			},
			// 同意成为区域经理（不选择区域）
			agreeManagerNoArea(row) {
				this.$confirm('确定审核通过?', {
          			type: 'warning'
        		}).then(() => {
        			this.loading = true;
        			var url = `${this.url}/spread/check_manager_ask.action`;
        			var params = {
        				be_saleman_id: row.be_saleman_id,
        				is_agree: 1
        			};
        			this.$http.get(url,{ params }).then((res)=> {
        				this.loading = false;
        				if( res.data.success ) {
        					this.fetchIsManager();
        				}else {
        					this.$message({
        						type: "error",
        						message: res.data.result.errmsg
        					});
        				}
        			});
		        });
			},
			//同意成为区域经理（选择区域）
			agreeManager(row) {
				this.be_saleman_id = row.be_saleman_id;
				this.showAgreeModel = true;
			},
			okAgreeManager(be_saleman_id, area_id) {
				this.showAgreeModel = false;
				this.loading = true;
				var url = `${this.url}/spread/check_manager_ask.action`;
				var params = {
					be_saleman_id: be_saleman_id,
					is_agree: 1,
					area_id: area_id
				};
				this.$http.get(url,{ params }).then((res)=> {
					this.loading = false;
					if( res.data.success ) {
						this.fetchIsManager();
					}else {
						this.$message({
							type: "error",
							message: res.data.result.errmsg
						});
					}
				});
			},
			//拒绝成为区域经理
			rejectManager(row) {
				this.$prompt("请填写拒绝理由", "拒绝申请", {
					cancelButtonText: '取消',
					confirmButtonText: "确定",
				}).then(({ value })=> {
					if (value === null || value.replace(/\s/g, "") === "") {
						this.$message({
							type: "error",
							message: "请输入拒绝理由"
						});
						return;
					}
					var url = `${this.url}/spread/check_manager_ask.action`;
					var params = {
						be_saleman_id: row.be_saleman_id,
						is_agree: 0,
						reject_reason: value,
					};
					this.$http.get(url,{ params }).then((res)=> {
						this.loading = true;
						if( res.data.success ) {
							this.fetchIsManager();
						}else {
							this.$message({
								type: "error",
								message: res.data.result.errmsg
							});
						}
					});
					
				}).catch(()=>{
					
				});
			}
		}

	}
</script>

<style scoped lang="scss">

</style>